package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.HashMap;

/**
 * Connection to the meetings table
 * 
 * Performs DAO operations for the meetings table in the database
 * @author b_aldosa
 * @since June 15 2013
 * @version $Revision: 1.0 $
 */
public class MeetingSqlDao implements MeetingDao {
	
	 
	// Connection
	private Connection conn;
    
    /**
     * Constructor
     * @param conn connection object for database
     */
    public MeetingSqlDao(Connection conn) {
		this.conn = conn;
	}
    
    /**
     * Creates a new entry and returns the ID
     * @param properties 
    
     * @return int
     * @throws DaoException
     * @throws ModelException  * @see dao.MeetingDao#create(HashMap<String,String>)
     */
    public int create(HashMap<String, String> properties) throws DaoException{
    	
    	int toReturn = -1;
    	try {
    		String sql = "INSERT INTO meetings (Owner, RevisionId, Topic, Location, Start, End, Attendees, Description) VALUES (?,?,?,?,?,?,?,?) ;";
	    	PreparedStatement statement = conn.prepareStatement(sql);
	    	statement.setString(1, properties.get("Owner"));
	    	statement.setString(2, properties.get("RevisionId"));
	    	statement.setString(3, properties.get("Topic"));
	    	statement.setString(4, properties.get("Location"));
	    	statement.setTimestamp(5, Timestamp.valueOf(properties.get("Start")));
	    	statement.setTimestamp(6, Timestamp.valueOf(properties.get("End")));
	    	statement.setString(7, properties.get("Attendees"));
	    	statement.setString(8, properties.get("Description"));
	    	statement.executeUpdate();
	    	Statement st = conn.createStatement();
        	ResultSet generatedKeys = st.executeQuery("SELECT seq FROM sqlite_sequence WHERE name = 'meetings' ;");
        	if (generatedKeys.next()) {
        	    toReturn = generatedKeys.getInt("seq");
        	}
    	} catch (SQLException e){
    		throw new DaoException(e.getMessage());
    	}
    	return toReturn;
    }
    
    /**
     * Loads a meeting row given ID
    
    
    
     * @param Id int
     * @return properties * @throws DaoException
     * @throws ModelException  * @see dao.MeetingDao#load(int)
     */
    public HashMap<String, String> load(int Id) throws DaoException{
    	
    	HashMap<String, String> toReturn = null;
    	try {
	    	String sql = "SELECT * FROM meetings WHERE Id = ? LIMIT 1; ";
	    	PreparedStatement statement = conn.prepareStatement(sql);
	    	statement.setInt(1, Id);
	    	ResultSet rs = statement.executeQuery();
	    	toReturn = new HashMap<String, String>();
	    	while (rs.next()) {
	    		toReturn.put("Id", String.valueOf(Id));
	    		toReturn.put("Owner", rs.getString("Owner"));
	    		toReturn.put("RevisionId", rs.getString("RevisionId"));
	    		toReturn.put("Topic", rs.getString("Topic"));
	    		toReturn.put("Location", rs.getString("Location"));
	    		toReturn.put("Start", rs.getString("Start"));
	    		toReturn.put("End", rs.getString("End"));
	    		toReturn.put("Attendees", rs.getString("Attendees"));
	    		toReturn.put("Description", rs.getString("Description"));
	    	}
    	} catch (SQLException e){
    		throw new DaoException(e.getMessage());
    	}
    	return toReturn;
    }

    /**
     * Updates a meeting
     * @param properties
    
     * @throws DaoException
     * @throws ModelException  * @see dao.MeetingDao#update(HashMap<String,String>)
     */
    public void update(HashMap<String, String> properties) throws DaoException {
    
    	
    	try {
    		String sql = "UPDATE meetings SET Owner=?, RevisionId=?, Topic = ?,Location = ?, Start = ?, " +
    				"End = ?, Attendees = ?, Description = ?  WHERE Id = ?; ";
	    	PreparedStatement statement = conn.prepareStatement(sql);

	    	statement.setString(1, properties.get("Owner"));
	    	statement.setString(2, properties.get("RevisionId"));
	    	statement.setString(3, properties.get("Topic"));
	    	statement.setString(4, properties.get("Location"));
	    	statement.setTimestamp(5, Timestamp.valueOf(properties.get("Start")));
	    	statement.setTimestamp(6, Timestamp.valueOf(properties.get("End")));
	    	statement.setString(7, properties.get("Attendees"));
	    	statement.setString(8, properties.get("Description"));
	    	statement.setInt(9, Integer.valueOf(properties.get("Id")));
	      	statement.executeUpdate();
	      	
    	} catch (SQLException e){
    		
    		throw new DaoException(e.getMessage());
    		
    	}
    }

	/**
	 * Deletes a user row, given the id
	
	
	 * @param Id int
	 * @throws DaoException
	 * @throws ModelException  * @see dao.MeetingDao#delete(int)
	 */
	public void delete(int Id) throws DaoException {
		
		try {
	    	String sql = "DELETE FROM meetings WHERE Id = ? ; ";
	    	PreparedStatement statement = conn.prepareStatement(sql);
	    	statement.setInt(1, Id);
	    	statement.executeUpdate();
    	} catch (SQLException e){
    		throw new DaoException(e.getMessage());
    	}
	}
	

	
}